package com.kx.kxblog.controller;

import com.kx.kxblog.annotation.DefaultResult;
import com.kx.kxblog.annotation.PermissionLimit;
import com.kx.kxblog.annotation.ResultController;
import com.kx.kxblog.bean.Group;
import com.kx.kxblog.bean.Result;
import com.kx.kxblog.entity.AbstractEntity;
import com.kx.kxblog.enums.ResultCode;
import com.kx.kxblog.exception.ResultException;
import com.kx.kxblog.service.AbstractService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@ResultController
public abstract class AbstractController<T extends AbstractEntity, ID,Service extends AbstractService<T,ID>> {

    @Autowired
    protected Service service;

    @GetMapping("/id/{id}")
    @ApiOperation(value="根据id查找")
    public T getById(@NotNull @ApiParam(value = "id",required = true) @PathVariable ID id){
        return service.getById(id);
    }

    @PostMapping
    @PermissionLimit
    @ApiOperation(value="添加")
    public T create(@Validated(Group.Insert.class) @RequestBody T entity){
        return service.save(entity);
    }

    @PutMapping
    @PermissionLimit
    @ApiOperation(value="修改")
    public T modifyById(@Validated(Group.ById.class) @RequestBody T entity){
        return service.save(entity);
    }

//    protected void setService(Service service){
//        this.service = service;
//    }
}
